package qlsl.androiddesign.http.service.commonservice;

import java.security.acl.Group;
import java.util.HashMap;
import java.util.List;

import qlsl.androiddesign.constant.SoftwareConstant;
import qlsl.androiddesign.constant.WhatConstant;
import qlsl.androiddesign.entity.commonentity.Pager;
import qlsl.androiddesign.entity.otherentity.Posting;
import qlsl.androiddesign.entity.otherentity.ShoppingDetail;
import qlsl.androiddesign.handler.subhandler.HttpHandler;
import qlsl.androiddesign.http.HttpListener;
import qlsl.androiddesign.http.service.baseservice.BaseService;
import qlsl.androiddesign.http.xutils.HttpProtocol;
import qlsl.androiddesign.thread.HandlerThread;
import qlsl.androiddesign.util.commonutil.PagerUtils;
import qlsl.androiddesign.view.baseview.FunctionView;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

/**
 * 搜索模块 <br/>
 * 
 */
public class SearchService extends BaseService {

	private static String className = getClassName(SearchService.class);

	public static void searchGroup(final String keyword, final int type,
			final int page, final FunctionView<?> functionView,
			final HttpListener listener) {
		final HttpHandler handler = getHandler(functionView, listener,
				className, "searchGroup");
		new HandlerThread(className, "searchGroup") {
			public void run() {
				HttpProtocol protocol = new HttpProtocol();
				try {
					JSONObject jo = protocol.setMethod("group_search")
							.addParam("keyword", keyword)
							.addParam("type", type).addParam("page", page)
							.addParam("size", SoftwareConstant.PAGER_SIZE)
							.get();
					if (isDataInvalid(jo, handler, protocol)) {
						return;
					}

					HashMap<String, Object> map = new HashMap<String, Object>();
					List<Group> list = JSONArray.parseArray(
							jo.getString("data"), Group.class);
					Pager pager = PagerUtils.createPager(page, jo);
					map.put("list", list);
					map.put("pager", pager);

					handler.sendMessage(protocol,
							WhatConstant.WHAT_NET_DATA_SUCCESS, map);
				} catch (Exception e) {
					e.printStackTrace();
					handler.sendMessage(protocol, WhatConstant.WHAT_EXCEPITON,
							e);
				}
			};
		}.start();
	}

	/**
	 * 搜素商品列表
	 * 
	 * @param page
	 * @param condtion
	 * @param functionView
	 * @param listener
	 */
	public static void searchShoppingName(final int page,
			final String condtion, final FunctionView<?> functionView,
			final HttpListener listener) {
		final HttpHandler handler = getHandler(functionView, listener,
				className, "searchShoppingName");
		new HandlerThread(className, "searchShoppingName") {
			public void run() {
				HttpProtocol protocol = new HttpProtocol();
				try {
					JSONObject jo = protocol.setMethod("getgoodslist")
							.addParam("condtion", condtion)
							.addParam("orderby", 0)
							.addParam("size", SoftwareConstant.PAGER_SIZE)
							.post();
					
					if (isDataInvalid(jo, handler, protocol)) {
						return;
					}
					
					List<ShoppingDetail> list = JSONArray.parseArray(
							jo.getString("rows"), ShoppingDetail.class);

					handler.sendMessage(protocol,
							WhatConstant.WHAT_NET_DATA_SUCCESS, list);
				} catch (Exception e) {
					e.printStackTrace();
					handler.sendMessage(protocol, WhatConstant.WHAT_EXCEPITON,
							e);
				}
			};
		}.start();
	}

	/**
	 * 搜索帖子列表
	 * 
	 * @param page
	 * @param condtion
	 * @param functionView
	 * @param listener
	 */
	public static void searchShoppingName2(final int page,
			final String condtion, final FunctionView<?> functionView,
			final HttpListener listener) {
		final HttpHandler handler = getHandler(functionView, listener,
				className, "searchShoppingName2");
		new HandlerThread(className, "searchShoppingName2") {
			public void run() {
				HttpProtocol protocol = new HttpProtocol();
				try {
					JSONObject jo = protocol.setMethod("getpostingslist")
							.addParam("condtion", condtion)
							.addParam("orderby", 0)
							.addParam("size", SoftwareConstant.PAGER_SIZE)
							.post();
					if (isDataInvalid(jo, handler, protocol)) {
						return;
					}

					HashMap<String, Object> map = new HashMap<String, Object>();
					List<Posting> list = JSONArray.parseArray(
							jo.getString("rows"), Posting.class);
					Pager pager = PagerUtils.createPager(page, jo);
					map.put("list", list);
					map.put("pager", pager);

					handler.sendMessage(protocol,
							WhatConstant.WHAT_NET_DATA_SUCCESS, map);
				} catch (Exception e) {
					e.printStackTrace();
					handler.sendMessage(protocol, WhatConstant.WHAT_EXCEPITON,
							e);
				}
			};
		}.start();
	}
}
